package com.ruoyi.file.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.constant.oldConstant.UserConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.JsonUtils;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.utils.oldUtils.SpringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.redis.constant.CacheNames;
import com.ruoyi.common.redis.utils.CacheUtils;
import com.ruoyi.common.redis.utils.RedisUtils;
import com.ruoyi.file.domain.SysOssConfig;
import com.ruoyi.file.domain.bo.SysOssConfigBo;
import com.ruoyi.file.domain.vo.SysOssConfigVo;
import com.ruoyi.file.mapper.SysOssConfigMapper;
import com.ruoyi.file.mapper.SysOssMapper;
import com.ruoyi.file.oss.constant.OssConstant;
import com.ruoyi.file.oss.factory.OssFactory;
import com.ruoyi.file.service.ISysOssConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 对象存储配置Service业务层处理
 *
 * @author Lion Li
 * @author 孤舟烟雨
 * @date 2021-08-13
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysOssConfigServiceImpl implements ISysOssConfigService {

	SysOssConfigMapper baseMapper;

	/**
	 * 项目启动时，初始化参数到缓存，加载配置类
	 */
	@Override
	public void init() {
		List<SysOssConfig> list = baseMapper.selectList();
		// 加载OSS初始化配置
		for (SysOssConfig config : list) {
			String configKey = config.getConfigKey();
			if ("0".equals(config.getStatus())) {
				RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, configKey);
			}
			SpringUtils.context().publishEvent(config);
		}
		// 初始化OSS工厂
		OssFactory.init();
	}

	@Override
	public SysOssConfigVo queryById(Long ossConfigId) {
		return baseMapper.selectVoById(ossConfigId);
	}

	@Override
	public TableDataInfo<SysOssConfigVo> queryPageList(SysOssConfigBo bo, PageQuery pageQuery) {
		LambdaQueryWrapper<SysOssConfig> lqw = buildQueryWrapper(bo);
		Page<SysOssConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

	private LambdaQueryWrapper<SysOssConfig> buildQueryWrapper(SysOssConfigBo bo) {
		LambdaQueryWrapper<SysOssConfig> lqw = Wrappers.lambdaQuery();
		lqw.eq(StringUtils.isNotBlank(bo.getConfigKey()), SysOssConfig::getConfigKey, bo.getConfigKey());
		lqw.like(StringUtils.isNotBlank(bo.getBucketName()), SysOssConfig::getBucketName, bo.getBucketName());
		lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysOssConfig::getStatus, bo.getStatus());
		return lqw;
	}

	@Override
	public Boolean insertByBo(SysOssConfigBo bo) {
		SysOssConfig config = BeanUtil.toBean(bo, SysOssConfig.class);
		validEntityBeforeSave(config);
		boolean flag = baseMapper.insert(config) > 0;
		if (flag) {
			SpringUtils.context().publishEvent(config);
		}
		return flag;
	}

	@Override
	public Boolean updateByBo(SysOssConfigBo bo) {
		SysOssConfig config = BeanUtil.toBean(bo, SysOssConfig.class);
		validEntityBeforeSave(config);
		LambdaUpdateWrapper<SysOssConfig> luw = new LambdaUpdateWrapper<>();
		luw.set(ObjectUtil.isNull(config.getPrefix()), SysOssConfig::getPrefix, "");
		luw.set(ObjectUtil.isNull(config.getRegion()), SysOssConfig::getRegion, "");
		luw.set(ObjectUtil.isNull(config.getExt1()), SysOssConfig::getExt1, "");
		luw.set(ObjectUtil.isNull(config.getRemark()), SysOssConfig::getRemark, "");
		luw.eq(SysOssConfig::getOssConfigId, config.getOssConfigId());
		boolean flag = baseMapper.update(config, luw) > 0;
		if (flag) {
			SpringUtils.context().publishEvent(config);
		}
		return flag;
	}

	/**
	 * 保存前的数据校验
	 */
	private void validEntityBeforeSave(SysOssConfig entity) {
		if (StringUtils.isNotEmpty(entity.getConfigKey())
				&& UserConstants.NOT_UNIQUE.equals(checkConfigKeyUnique(entity))) {
			throw new ServiceException("操作配置'" + entity.getConfigKey() + "'失败, 配置key已存在!");
		}
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			if (CollUtil.containsAny(ids, OssConstant.SYSTEM_DATA_IDS)) {
				throw new ServiceException("系统内置, 不可删除!");
			}
		}
		List<SysOssConfig> list = CollUtil.newArrayList();
		for (Long configId : ids) {
			SysOssConfig config = baseMapper.selectById(configId);
			list.add(config);
		}
		boolean flag = baseMapper.deleteBatchIds(ids) > 0;
		if (flag) {
			list.forEach(sysOssConfig -> CacheUtils.evict(CacheNames.SYS_OSS_CONFIG, sysOssConfig.getConfigKey()));
		}
		return flag;
	}

	/**
	 * 判断configKey是否唯一
	 */
	private String checkConfigKeyUnique(SysOssConfig sysOssConfig) {
		long ossConfigId = ObjectUtil.isNull(sysOssConfig.getOssConfigId()) ? -1L : sysOssConfig.getOssConfigId();
		SysOssConfig info = baseMapper.selectOne(
				new LambdaQueryWrapper<SysOssConfig>().select(SysOssConfig::getOssConfigId, SysOssConfig::getConfigKey)
						.eq(SysOssConfig::getConfigKey, sysOssConfig.getConfigKey()));
		if (ObjectUtil.isNotNull(info) && info.getOssConfigId() != ossConfigId) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	/**
	 * 启用禁用状态
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateOssConfigStatus(SysOssConfigBo bo) {
		SysOssConfig sysOssConfig = BeanUtil.toBean(bo, SysOssConfig.class);
		int row = baseMapper.update(null, new LambdaUpdateWrapper<SysOssConfig>().set(SysOssConfig::getStatus, "1"));
		row += baseMapper.updateById(sysOssConfig);
		if (row > 0) {
			RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, sysOssConfig.getConfigKey());
		}
		return row;
	}

	/**
	 * 更新配置缓存
	 *
	 * @param config 配置
	 */
	@EventListener
	public void updateConfigCache(SysOssConfig config) {
		CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config));
		RedisUtils.publish(OssConstant.DEFAULT_CONFIG_KEY, config.getConfigKey(), msg -> {
			log.info("发布刷新OSS配置 => " + msg);
		});
	}

	@Override
	public SysOssConfig getDefaultConfig() {
		//TODO 暂时写死
//		String key = RedisUtils.getCacheObject(OssConstant.DEFAULT_CONFIG_KEY);
//		String config = CacheUtils.get(CacheNames.SYS_OSS_CONFIG, key);
		String config = "{\"createBy\":\"admin\",\"createTime\":\"2023-03-06 13:42:30\",\"updateBy\":\"1\",\"updateTime\":\"2023-04-25 08:44:46\",\"ossConfigId\":3,\"configKey\":\"aliyun\",\"accessKey\":\"LTAI5t71VH1HDh5iwKhYU3AA\",\"secretKey\":\"Kgp2B5hXMI5LMWwHzUvwSpYxNVOIm9\",\"bucketName\":\"erp-aliexpress\",\"prefix\":\"common-product\",\"endpoint\":\"oss-cn-beijing.aliyuncs.com\",\"domain\":\"https://file.pbb2c.com\",\"isHttps\":\"N\",\"region\":\"oss-cn-beijing\",\"status\":\"0\",\"ext1\":\"\",\"remark\":\"\",\"accessPolicy\":\"1\"}";
		SysOssConfig sysOssConfig = JSONObject.parseObject(config, SysOssConfig.class);
		return sysOssConfig;
	}
}
